﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using BO = Viewer.BusinessObject.BusinessEntities;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class QUNhomQuyenByModule : BusinessBase
    {
        public string TenNhomQuyen { get; set; }
        public string TenUserOwner { get; set; }
        public string GetTenNhomQuyen(int NhomQuyenByModuleID)
        {
            string result = string.Empty;

            List<QUModule> queryModule = (from a in this.Context.QUNhomQuyenByModule_Modules
                                          join b in this.Context.QUModules on a.ModuleID equals b.ModuleID
                                          where a.NhomQuyenByModuleID.Equals(NhomQuyenByModuleID)
                                          select b).ToList();
            foreach (QUModule item in queryModule)
            {
                if (result != string.Empty)
                {
                    result = string.Format("{0}, {1}", result, item.TenModuleFull);
                }
                else
                {
                    result = item.TenModuleFull;
                }
            }

            return result;
        }
        public List<QUNhomQuyenByModule> LoadDataModuleByIdUser(int id)
        {
            var queryId = from a in this.Context.QUNhomQuyenByModule_NguoiDungs
                          where a.QUNguoiDung.NguoiDungID.Equals(id)
                          select a.NhomQuyenByModuleID;
            var queryModule = from a in this.Context.QUNhomQuyenByModules
                              where queryId.Contains(a.NhomQuyenByModuleID)
                              select a;
            return queryModule.ToList();

        }
        public List<QUNhomQuyenByModule> GetAll()
        {         
            List<QUNhomQuyenByModule> lst = this.Context.QUNhomQuyenByModules.ToList();
            if (lst!=null)
            {
                foreach (QUNhomQuyenByModule item in lst)
                {
                    item.TenNhomQuyen = GetTenNhomQuyen(item.NhomQuyenByModuleID);
                    item.TenUserOwner = new BO.QUNguoiDung().LoadDataById(item.UserIDOwner).TenDangNhap;
                }
            }
         
            return lst;
        }

        public List<QUNhomQuyenByModule> GetAllWithCurrentUserOwner()
        {
            List<QUNguoiDung> nguoiDung = new BO.QUNguoiDung().SearchNguoiDung(HttpContext.Current.User.Identity.Name);
            List<QUNhomQuyenByModule> lst = this.Context.QUNhomQuyenByModules.Where(c => c.UserIDOwner == nguoiDung[0].NguoiDungID).ToList();
            foreach (QUNhomQuyenByModule item in lst)
            {
                item.TenNhomQuyen = GetTenNhomQuyen(item.NhomQuyenByModuleID);
                item.TenUserOwner = new BO.QUNguoiDung().LoadDataById(item.UserIDOwner).TenDangNhap;
            }
            return lst;
        }
        public QUNhomQuyenByModule GetById(int pNhomQuyenByModuleID)
        {
            QUNhomQuyenByModule nhomQuyen = this.Context.QUNhomQuyenByModules.Where(c => c.NhomQuyenByModuleID == pNhomQuyenByModuleID).FirstOrDefault();
            nhomQuyen.TenUserOwner = new QUNguoiDung().LoadDataById(nhomQuyen.UserIDOwner).TenDangNhap;
            return nhomQuyen;
        }

        public void Save()
        {
            if (this.NhomQuyenByModuleID == 0)
            {
                //addNew
                this.Context.QUNhomQuyenByModules.InsertOnSubmit(this);
                this.Context.SubmitChanges();
            }
            else
            {
                //edit
                var oItem = this.GetById(this.NhomQuyenByModuleID);
                oItem.TenNhomQuyenByModule = this.TenNhomQuyenByModule;
                oItem.GhiChu = this.GhiChu;

                foreach (var item in oItem.QUNhomQuyenByModule_NguoiDungs)
                {
                    this.Context.QUNhomQuyenByModule_NguoiDungs.DeleteOnSubmit(item);
                }

                foreach (var item in oItem.QUNhomQuyenByModule_Modules)
                {
                    this.Context.QUNhomQuyenByModule_Modules.DeleteOnSubmit(item);
                }

                QUNhomQuyenByModule_NguoiDung oNguoiDung;

                foreach (var item in this.QUNhomQuyenByModule_NguoiDungs)
                {
                    oNguoiDung = new QUNhomQuyenByModule_NguoiDung();
                    oNguoiDung.NguoiDungID = item.NguoiDungID;
                    oNguoiDung.NhomQuyenByModuleID = item.NhomQuyenByModuleID;

                    oItem.QUNhomQuyenByModule_NguoiDungs.Add(oNguoiDung);
                }

                QUNhomQuyenByModule_Module newItem2;
                foreach (var item in this.QUNhomQuyenByModule_Modules)
                {
                    newItem2 = new QUNhomQuyenByModule_Module();
                    newItem2.NhomQuyenByModuleID = item.NhomQuyenByModuleID;
                    newItem2.ModuleID = item.ModuleID;
                    newItem2.IsView = item.IsView;
                    newItem2.IsAdd = item.IsAdd;
                    newItem2.IsEdit = item.IsEdit;
                    newItem2.IsDelete = item.IsDelete;
                    newItem2.IsModuleConThuaKe = item.IsModuleConThuaKe;

                    oItem.QUNhomQuyenByModule_Modules.Add(newItem2);
                }

                this.Context.SubmitChanges();
            }
        }
        public void Delete()
        {
            var oItem = this.GetById(this.NhomQuyenByModuleID);
            foreach (var item in oItem.QUNhomQuyenByModule_NguoiDungs)
            {
                this.Context.QUNhomQuyenByModule_NguoiDungs.DeleteOnSubmit(item);
            }

            foreach (var item in oItem.QUNhomQuyenByModule_Modules)
            {
                this.Context.QUNhomQuyenByModule_Modules.DeleteOnSubmit(item);
            }
            this.Context.QUNhomQuyenByModules.DeleteOnSubmit(oItem);
            this.Context.SubmitChanges();
        }
    }
}